----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_PBEULAPopup = {}

----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

----------------------------------------------------------------
-- EA_Window_PBEULAPopup Functions
----------------------------------------------------------------

function EA_Window_PBEULAPopup.Initialize()

    -- since the window is hidden when it is initialized we are good to set up the window
    LabelSetText( "EA_Window_PBEULAPopupTitleBarText", GetPregameString( StringTables.Pregame.LABEL_PBEULA_TITLE))
    ButtonSetText("EA_Window_PBEULAPopupAcceptButton", GetPregameString( StringTables.Pregame.LABEL_PRESELECT_ACCEPT ))
    ButtonSetText("EA_Window_PBEULAPopupDeclineButton", GetPregameString( StringTables.Pregame.LABEL_PRESELECT_DECLINE ))
    LabelSetText( "EA_Window_PBEULAPopupAgreeCheckboxLabel", GetPregameString( StringTables.Pregame.LABEL_PBEULA_CHECKBOX))
    ButtonSetCheckButtonFlag("EA_Window_PBEULAPopupAgreeCheckbox", true)

    -- disable the accept button and the checkbox initially
    ButtonSetDisabledFlag("EA_Window_PBEULAPopupAcceptButton", true)

    EA_Window_PBEULAPopup.CreateTextBody()
end

function EA_Window_PBEULAPopup.Shutdown()
    TextLogDestroy("PBEULALog")
end

function EA_Window_PBEULAPopup.Show()
    WindowSetShowing("EA_Window_PBEULA", true)
end

function EA_Window_PBEULAPopup.ToggleShowing()
    local showing = WindowGetShowing("EA_Window_PBEULA")
    WindowSetShowing("EA_Window_PBEULA", not showing)
end

function EA_Window_PBEULAPopup.Accept()
    -- don't do anything if the checkbox hasn't been checked
    if (ButtonGetDisabledFlag("EA_Window_PBEULAPopupAcceptButton"))
    then
        return
    end

    -- we only show the punk buster EULA once so we set it to true here and it is saved out in the user settings
    GameData.Account.Agreed2PunkBuster = true

    -- now we can do the performance warning
    SettingsWindowTabbed.DoLoginPerformanceWarning()

    WindowSetShowing("EA_Window_PBEULA", false)
end

function EA_Window_PBEULAPopup.Decline()
    WindowSetShowing("EA_Window_PBEULA", false)
    -- if you don't accept then we will quit the game
    BroadcastEvent( SystemData.Events.QUIT )
end

function EA_Window_PBEULAPopup.CreateTextBody()
    TextLogDestroy("PBEULALog")
    TextLogCreate("PBEULALog", #StringTables.PBEULA)
    TextLogAddFilterType("PBEULALog", 1, L"")
    LogDisplayAddLog("EA_Window_PBEULAPopupLogDisplay", "PBEULALog", true)
    LogDisplaySetShowTimestamp("EA_Window_PBEULAPopupLogDisplay", false)
    LogDisplaySetShowLogName("EA_Window_PBEULAPopupLogDisplay", false)
    TextLogDisplayShowScrollbar("EA_Window_PBEULAPopupLogDisplay", true)

    for index = 0, #StringTables.PBEULA
    do
        TextLogAddEntry("PBEULALog", 1, GetStringFromTable("PBEULA", index))
    end

    LogDisplayScrollToTop("EA_Window_PBEULAPopupLogDisplay")
end

function EA_Window_PBEULAPopup.ToggleAgreeCheckbox()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_PBEULAPopupAgreeCheckbox"))
    then
        return
    end

    local toggleIsPressed = ButtonGetPressedFlag("EA_Window_PBEULAPopupAgreeCheckbox")
    ButtonSetDisabledFlag("EA_Window_PBEULAPopupAcceptButton", not toggleIsPressed)
end

function EA_Window_PBEULAPopup.ToggleAgreeCheckboxFromLabel()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_PBEULAPopupAgreeCheckbox"))
    then
        return
    end

    local toggleIsPressed = ButtonGetPressedFlag("EA_Window_PBEULAPopupAgreeCheckbox")
    ButtonSetPressedFlag("EA_Window_PBEULAPopupAgreeCheckbox", not toggleIsPressed)
    EA_Window_PBEULAPopup.ToggleAgreeCheckbox()
end

function EA_Window_PBEULAPopup.MouseOverCheckboxLabel()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_PBEULAPopupAgreeCheckbox"))
    then
        return
    end

    LabelSetTextColor("EA_Window_PBEULAPopupAgreeCheckboxLabel", DefaultColor.YELLOW.r, DefaultColor.YELLOW.g, DefaultColor.YELLOW.b)
end

function EA_Window_PBEULAPopup.MouseOverEndCheckboxLabel()
    -- don't do anything if the checkbox hasn't been enabled
    if (ButtonGetDisabledFlag("EA_Window_PBEULAPopupAgreeCheckbox"))
    then
        return
    end

    LabelSetTextColor("EA_Window_PBEULAPopupAgreeCheckboxLabel", DefaultColor.WHITE.r, DefaultColor.WHITE.g, DefaultColor.WHITE.b)
end
